
ALTER procedure sp_HopDong_CheckDaiLyTrung
@MaDaiLy varchar(4)
as
-- CHECK TRUNG MA DAI LY, TRA VE 1 NEU TRUNG, TRA VE 0 NEU KO TRUNG
	IF EXISTS( SELECT * FROM(	
			SELECT B.MaDLMoi AS Ma_DaiLy 
				FROM tblChuyenDL B
				WHERE Ngay = (SELECT MAX(NGAY) 
								FROM tblChuyenDL 
								WHERE HopDongID = B.HopDongID )
			UNION 
			SELECT A.Ma_DaiLy FROM HOPDONG  A
			) DaiLy
			where Ma_DaiLy = @MaDaiLy)
	BEGIN	SELECT 1	END
	ELSE	BEGIN	SELECT 0	END
GO